package com.centling.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.centling.business.CustomerBean;
import com.centling.controller.model.CustomerVO;
import com.centling.service.IAxis2Service;
import com.centling.service.ICustomerService;
import com.centling.service.IUtilsService;
import com.centling.util.Encryption;
import com.centling.util.LocalUtils;
import com.centling.util.SmsSender;

/**
 * 1.	客户信息（电商）导入接口
 * 2.	客户信息（电商）取得接口
 * 
 * @author John.Ju
 *
 */
@Controller
@Transactional
public class CustomerController {
	Logger log = Logger.getLogger(CustomerController.class);
	
	public static String RC_CRM_KEY = "RCCRMKEY";
	
	@Resource
	private ICustomerService customerService;
	
	@Resource
	private IUtilsService utilsService;
	
	@Resource
	private IAxis2Service axis2Service;
	
	@RequestMapping(value = "/indexShow.do", method = RequestMethod.GET)
	public String showIndexPage(HttpServletRequest request) {
		/*//customerService.findAll();
		List<CustomerBean> list = new ArrayList<CustomerBean>();
		CustomerBean cb = new CustomerBean();
		cb.setF_system_source("电商");
		//cb.setfNumber("DSKH00100");
		cb.setfName("centling");
		cb.setF_customer_source("电商");
		cb.setFsex("1");
		cb.setfBirthday(new Date());
		cb.setfAge(0);
		cb.setFnation("汉");
		cb.setF_persional_faith("中国共产党");
		cb.setF_personal_taboo("无");
		// TODO 改成ID的形式
		cb.setF_education("本科");
		cb.setF_marital_date(new Date());
		cb.setF_memorial_date(new Date());
		cb.setF_memorial_name("哈哈");
		// TODO 改成ID的形式
		cb.setfComboBox8("巨蟹座");
		// TODO 改成ID的形式
		cb.setfComboBox9("O");
		*//** 个人-联系信息 *//*
		cb.setfCountry("中国");
		cb.setfRegionID(LocalUtils.getRegionIDByRegionName("东北区"));
		cb.setfProvinceID(LocalUtils.getProvinceIDByProvinceName("山东省"));
		cb.setfCityID(utilsService.getCityIDByCityName("青岛市"));
		cb.setfAddress("东北区山东省青岛市");
		cb.setfPhone("110");
		private String	fAddress;		//通讯地址		直接填写	自动取省份+城市+手工填写内容
		private String	fPhone;				//电话		直接填写
		private String	fMobile;			//移动电话		直接填写
		private String	fEmail;				//E-mail	直接填写
		private String	f_QQ;				//QQ		直接填写
		private String	f_skype;			//SKYPE		直接填写
		private String	f_micro_message;	//微信		直接填写
		private String	f_micro_blog;		//微博		直接填写
		*//** 个人-单位信息 *//*
		private String	fWorkUnit;		//工作单位		直接填写
		cb.setfTrade(LocalUtils.getTradeIDByTradeName("制造业"));
		private Integer	fTrade;				//行业		下拉菜单
		private String	f_work;				//职业		直接填写
		private String	fDuty;				//职务		直接填写
		private String	fWorkSite;			//工作地址		直接填写
		private String	fBizPhone;			//办公电话		直接填写
		private String	fFax;				//传真		直接填写
		// TODO 改成ID的形式
		private String	f_company_size;		//公司规模		下拉菜单	定义好公司规模的区间，待选
		private String	fHomePage;			//公司网址		直接填写
		*//** 个人-业务信息 *//*
		private String	f_dressing_specification;//日常着装规格	下拉菜单	正常、宽松、修身
		*//** 个人-其他信息  *//*
		private String	f_remarks;			//备注		长文本-直接填写
		
		
		list.add(cb);
		customerService.save(list);*/
		//log.info(axis2Service.getOrdersFromBL("2013-12-16", "2013-12-16", ""));
		SmsSender.send("15863050069", "helloju");
		return "index";
	}
	
	/**
	 * 1.	客户信息（电商）导入接口
	 * 
	 * 说明：	电商客户注册成功后，电商调用此接口将客户信息导入到CRM系统中
	 * 
	 * @param request
	 * @param response
	 * @param encryptionJsonStr
	 * @return
	 * 
	 * 成功返回：	Y
	 * 异常返回：	N
	 */
	@RequestMapping("/customerImportCRM.do")
	@ResponseBody
	@Transactional
	public String ecCustomerImport(HttpServletRequest request,HttpServletResponse response,
			@RequestParam String encryptionJsonStr) {	
		log.info("function ecCustomerImport start..., with param encryptionJsonStr:"+encryptionJsonStr);
		Boolean returnVal = false;
		try {
			String jsonStr=Encryption.decrypt(encryptionJsonStr, CustomerController.RC_CRM_KEY); 
			log.info("param after: " + jsonStr);
			JSONArray jsonArr = JSON.parseArray(jsonStr);
			List<CustomerBean> list = new ArrayList<CustomerBean>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for (int i=0; i<jsonArr.size(); i++){
				CustomerVO vo = JSON.toJavaObject(jsonArr.getJSONObject(i), CustomerVO.class);
				CustomerBean cb = new CustomerBean();
				cb.setF_system_source(vo.getF_system_source());
				cb.setF_other_customer_code(vo.getF_other_customer_code());
				cb.setfName(vo.getfName());
				cb.setF_customer_source(vo.getF_customer_source());
				cb.setF_customer_type((vo.getF_customer_type()!=null&&vo.getF_customer_type()==1)?"是":"否");
				if(LocalUtils.isNotNull(vo.getFsex()))cb.setFsex(vo.getFsex().toString());
				if(LocalUtils.isNotBlank(vo.getfBirthday())){
					Date date = (Date)sdf.parse(vo.getfBirthday());
					cb.setfBirthday(date);
					cb.setfAge(LocalUtils.calTwoTimeSpace(date, new Date(), Calendar.YEAR));
				}
				if(LocalUtils.isNotBlank(vo.getFnation()))cb.setFnation(vo.getFnation());
				if(LocalUtils.isNotBlank(vo.getF_persional_faith()))cb.setF_persional_faith(vo.getF_persional_faith());
				if(LocalUtils.isNotBlank(vo.getF_personal_taboo()))cb.setF_personal_taboo(vo.getF_personal_taboo());
				if(LocalUtils.isNotBlank(vo.getF_education()))cb.setF_education(vo.getF_education());
				if(LocalUtils.isNotBlank(vo.getF_marital_status()))cb.setF_marital_status(vo.getF_marital_status());
				if(LocalUtils.isNotNull(vo.getF_marital_date()))cb.setF_marital_date((Date)sdf.parse(vo.getF_marital_date()));
				if(LocalUtils.isNotBlank(vo.getF_memorial_name()))cb.setF_memorial_name(vo.getF_memorial_name());
				if(LocalUtils.isNotBlank(vo.getF_memorial_date()))cb.setF_memorial_date((Date)sdf.parse(vo.getF_memorial_date()));
				if(LocalUtils.isNotBlank(vo.getfComboBox8()))cb.setfComboBox8(vo.getfComboBox8());
				if(LocalUtils.isNotBlank(vo.getfComboBox9()))cb.setfComboBox9(vo.getfComboBox9());
				if(LocalUtils.isNotBlank(vo.getfCountry()))cb.setfCountry(vo.getfCountry());
				if(LocalUtils.isNotBlank(vo.getF_province()))cb.setF_province(vo.getF_province());
				if(LocalUtils.isNotBlank(vo.getF_city()))cb.setF_city(vo.getF_city());
				if(LocalUtils.isNotBlank(vo.getfRegionName()))cb.setfRegionID(LocalUtils.getRegionIDByRegionName(vo.getfRegionName()));
				String faddress = "";
				if(LocalUtils.isNotBlank(vo.getfProvinceName())){
					Integer provinceid = LocalUtils.getProvinceIDByProvinceName(vo.getfProvinceName());
					cb.setfProvinceID(provinceid);
					faddress += provinceid==null?"":vo.getfProvinceName();
				}
				
				if(LocalUtils.isNotBlank(vo.getfCityName())){
					Integer cityid = utilsService.getCityIDByCityName(vo.getfCityName());
					cb.setfCityID(cityid);
					faddress += cityid==null?"":vo.getfCityName();
				}
				if(LocalUtils.isNotBlank(vo.getfAddress()))faddress += vo.getfAddress();
				if(LocalUtils.isNotBlank(faddress))cb.setfAddress(faddress);
				if(LocalUtils.isNotBlank(vo.getfPhone()))cb.setfPhone(vo.getfPhone());
				if(LocalUtils.isNotBlank(vo.getfMobile()))cb.setfMobile(vo.getfMobile());
				if(LocalUtils.isNotBlank(vo.getfEmail()))cb.setfEmail(vo.getfEmail());
				if(LocalUtils.isNotBlank(vo.getF_QQ()))cb.setF_QQ(vo.getF_QQ());
				if(LocalUtils.isNotBlank(vo.getF_skype()))cb.setF_skype(vo.getF_skype());
				if(LocalUtils.isNotBlank(vo.getF_micro_message()))cb.setF_micro_message(vo.getF_micro_message());
				if(LocalUtils.isNotBlank(vo.getF_micro_blog()))cb.setF_micro_blog(vo.getF_micro_blog());
				if(LocalUtils.isNotBlank(vo.getfWorkUnit()))cb.setfWorkUnit(vo.getfWorkUnit());
				if(LocalUtils.isNotBlank(vo.getfTradeName()))cb.setfTrade(LocalUtils.getTradeIDByTradeName(vo.getfTradeName()));
				if(LocalUtils.isNotBlank(vo.getF_work()))cb.setF_work(vo.getF_work());
				if(LocalUtils.isNotBlank(vo.getfDuty()))cb.setfDuty(vo.getfDuty());
				if(LocalUtils.isNotBlank(vo.getfWorkSite()))cb.setfWorkSite(vo.getfWorkSite());
				if(LocalUtils.isNotBlank(vo.getfBizPhone()))cb.setfBizPhone(vo.getfBizPhone());
				if(LocalUtils.isNotBlank(vo.getfFax()))cb.setfFax(vo.getfFax());
				if(LocalUtils.isNotBlank(vo.getF_company_size()))cb.setF_company_size(vo.getF_company_size());
				if(LocalUtils.isNotBlank(vo.getfHomePage()))cb.setfHomePage(vo.getfHomePage());
				if(LocalUtils.isNotBlank(vo.getF_dressing_specification()))cb.setF_dressing_specification(vo.getF_dressing_specification());
				if(LocalUtils.isNotBlank(vo.getF_remarks()))cb.setF_remarks(vo.getF_remarks());
				list.add(cb);
			}
			returnVal = customerService.save(list);
			log.info("function ecCustomerImport end..., with return " + returnVal);
			return returnVal.toString();
		} catch (Exception e) {
			log.info("function ecCustomerImport end..., with return " + returnVal);
			log.error("function ecCustomerImport exception..., with error: " + e.getMessage());
			e.printStackTrace();
			return returnVal.toString();
		}
	}
	
	/**
	 * 2.	客户信息（电商）取得接口
	 * 说明：	
	 * 
	 * @param request
	 * @param response
	 * @param encryptionJsonStr
	 * @return
	 */
	@RequestMapping("/getEcCustomer.do")
	@ResponseBody
	public String getEcCustomer(HttpServletRequest request,HttpServletResponse response,
			@RequestParam String encryptionJsonStr) {
		
		return "yes";
	}
}
